package com.ruoyi.quartz.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;

public class Test{
    public static void main(String[] args) throws Exception{
        Long timestamp = System.currentTimeMillis();
        String secret = "生日this is secret";

        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
        System.out.println(sign);


        String url = "https://oapi.dingtalk.com/robot/send?access_token=04fa5e230dc83ff68adcee7702bb34732b5d8bd35371d6936213db75540a14b5&timestamp=%s&sign=%s";
        String format = String.format(url, timestamp, sign);
        RestTemplate template = new RestTemplate();
        System.out.println(format);
       String stringResponseEntity = template.postForObject(format,null, String.class);
        System.out.println(stringResponseEntity);


    }

}